package cs236369.webserver;

import java.util.HashMap;

import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.apache.http.impl.DefaultHttpServerConnection;


/*
 * 
 * The uri Can be accessed through : conn.receiveRequestHeader().getRequestLine().getUri().
 * The client socket can be accessed the way we did it in the connection listener.
 * The query parameters can be accessed through: conn.receiveRequestHeader().getParams().
 * 
 */

class Request {
	private HttpRequest httpRequest;
	private Header[] headers;
	private String path;
	private String method;
	private String uri;
	private String ext;
	private HashMap<String,String> params;
	DefaultHttpServerConnection conn;

	Request(DefaultHttpServerConnection _conn, HttpRequest _httpRequest, Config config, HashMap<String, String> _params) {
		httpRequest = _httpRequest;
		conn = _conn;
		params = _params;
		try {
			headers = httpRequest.getAllHeaders();
		} catch (Exception e) {
			Log.err("Request: Headers "+ e.getStackTrace());
		}
		String originalPath = httpRequest.getRequestLine().getUri();
		path = getAbsolutePath(config.welcomeFile, originalPath, config.base);
		method = httpRequest.getRequestLine().getMethod();
		Log.details(method);
		uri = httpRequest.getRequestLine().getUri();
		Log.out("Request: New request created");
		ext = path.substring(path.lastIndexOf(".") + 1, path.length());
	}

	private String getAbsolutePath(String welcomeFile, String originalPath, String base) {
		String relPath = null;
		//if path is to welcomeFile
		if (originalPath.equals("/") || originalPath.startsWith("/?") || originalPath.startsWith("/#")) {
			if (welcomeFile.startsWith("/")) {
				relPath = welcomeFile;
			} else {
				relPath = "/" + welcomeFile;
			}
		} else if (originalPath.contains("://")) {
			//if path is not relative
			int i = originalPath.indexOf(".");
			if (i == -1) {
				return null;
			}
			int j = originalPath.substring(i+1).indexOf("/");
			if (j == -1) {
				return null;
			}
			relPath = originalPath.substring(i+j);
		} else {
			// path relative
			if (originalPath.startsWith("/")) {
				relPath = originalPath;
			} else {
				relPath = "/" + originalPath;
			}
		}
		// normalizing base
		String basePath = base;
		if (base.endsWith("\\")) {
			basePath = base.substring(0,base.length()-1); 
		}
		if (relPath.indexOf("?") > -1) {
			relPath =relPath.substring(0,relPath.indexOf("?"));
		}
		if (relPath.indexOf("#") > -1) {
			relPath =relPath.substring(0,relPath.indexOf("#"));
		}
		return basePath + relPath.replace("/","\\");
	}

	HttpRequest getHttpRequest() {
		return httpRequest;
	}
	
	Header[] getHeaders() {
		return headers;
	}
	
	String getPath() {
		return path;
	}
	
	String getUri() {
		return uri;
	}
	
	String getMethod() {
		return method;
	}
	
	String getExt() {
		return ext;
	}
	
	DefaultHttpServerConnection getConnection() {
		return conn;
	}
	HashMap<String, String> getParams() {
		return params;
	}
}
